//
//  BeautyLightHomeViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/5/22.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "BeautyLightHomeViewController.h"
#import "BeautyInViewController.h"
#import "MyCourseViewController.h"
#import "UploadViewModel.h"
#import "MyDeviceListViewController.h"
#import "DeviceViewModel.h"
#import "SmartLinkExportObject.h"
#import "WifiUtils.h"
#import "UDPManage.h"
#import "HelpListViewController.h"
#import "GMWKWebViewController.h"
#import <Reachability/Reachability.h>
@interface BeautyLightHomeViewController ()<DeviceScanDelegate>{
    
}
@property(nonatomic,weak)IBOutlet UIButton *linkBtn;
@property(nonatomic,weak)IBOutlet UILabel *totalCount;
@property(nonatomic,weak)IBOutlet UILabel *totalTime;
@property(nonatomic,strong)UIScrollView *bannerScroll;
@property(nonatomic,strong)DeviceViewModel *deviceInfoViewModel;
@property(nonatomic,strong)DeviceListModel *currentDeviceModel;
@property(nonatomic,strong)SmartLinkExportObject *linkObj;
@property(nonatomic,strong)DeviceContinueModel *continueModel;
@end

@implementation BeautyLightHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBarHidden = YES;
    self.linkBtn.layer.cornerRadius = 30.5;
    [self setupUI];
    NSLog(@"____________________%@",[self internetStatus]);
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.deviceInfoViewModel fetchDeviceList:[LoginTools sharedTools].userId cmp:^(NSMutableArray * _Nonnull deviceList) {
        if ([deviceList count]==0) {
            self.currentDeviceModel = nil;
            self.totalCount.text = @"0次";
            self.totalTime.text = @"0min";
            [self.linkBtn setTitle:@"连接设备" forState:UIControlStateNormal];
        }else{
            for (DeviceListModel *deviceModel in deviceList) {
                if ([deviceModel.deviceStatus intValue]==0&&deviceModel.online==1) {
                    self.currentDeviceModel = deviceModel;
                    [self.linkBtn setTitle:@"开始美光" forState:UIControlStateNormal];
                    [self getCurrentDeviceInfo];
                }else{
                    self.currentDeviceModel = nil;
                    self.totalCount.text = @"0次";
                    self.totalTime.text = @"0min";
                    [self.linkBtn setTitle:@"连接设备" forState:UIControlStateNormal];
                }
            }
        }
    }];
}
-(void)getCurrentDeviceInfo{
    [self.deviceInfoViewModel fetchDeviceInfo:[NSString stringWithFormat:@"MAC:%@",[self.currentDeviceModel.deviceMac stringByReplacingOccurrencesOfString:@":" withString:@""]] cmp:^(NSDictionary * _Nonnull deviceInfo) {
        NSString *totalCountValue = [NSString stringWithFormat:@"%@",[deviceInfo objectForKey:@"totalUseCount"]];
        NSString *totalTimeValue = [NSString stringWithFormat:@"%@",[deviceInfo objectForKey:@"totalTime"]];
        if (!EC_ISNullStr(totalCountValue)) {
            self.totalCount.text = [NSString stringWithFormat:@"%@次",[deviceInfo objectForKey:@"totalUseCount"]];
        }else{
            self.totalCount.text = @"0次";
        }
        if (!EC_ISNullStr(totalTimeValue)) {
            self.totalTime.text = [NSString stringWithFormat:@"%@min",[deviceInfo objectForKey:@"totalTime"]];
        }else{
            self.totalTime.text = @"0min";
        }
    }];
    [self.deviceInfoViewModel getDeviceRemainTime:[NSString stringWithFormat:@"%.0f",self.currentDeviceModel.internalBaseClassIdentifier] cmp:^(DeviceContinueModel * _Nonnull model) {
        self.continueModel = model;
        if ([self.continueModel.status isEqualToString:@"1"]||[self.continueModel.status isEqualToString:@"2"]) {
            [self.linkBtn setTitle:@"继续美光" forState:UIControlStateNormal];
        }
    }];
}
-(void)setupUI{
    [self.view addSubview:self.bannerScroll];
    [self.bannerScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.linkBtn.mas_bottom).offset(10);
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.mas_equalTo(130);
    }];
    [self.bannerScroll layoutIfNeeded];
    UIImageView *firstBg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH-40, self.bannerScroll.height)];
    [firstBg setImage: [[UIImage imageNamed:@"cornerBg"] stretchableImageWithLeftCapWidth:169.5 topCapHeight:93.5]];
    [self.bannerScroll addSubview:firstBg];
    
    {
        UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(26, 130/2-120/2, 113, 120)];
        [icon setImage:[UIImage imageNamed:@"leading_first"]];
        [firstBg addSubview:icon];
        
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(168, 26, 90, 23)];
        title.font = [UIFont fontWithName:@"PingFang-SC-SemiBold" size:24];
        title.textColor = Color(@"333333");
        title.text = @"第一步";
        [firstBg addSubview:title];
        
        UILabel *subtitle = [[UILabel alloc]initWithFrame:CGRectMake(168, 73, 150, 17)];
        subtitle.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:16];
        subtitle.textColor = Color(@"333333");
        subtitle.text = @"设置时间，开始光疗";
        [firstBg addSubview:subtitle];
    }
    
    UIImageView *secondBg = [[UIImageView alloc]initWithFrame:CGRectMake(DEVICE_WIDTH-40, 0, DEVICE_WIDTH-40, self.bannerScroll.height)];
    [secondBg setImage: [[UIImage imageNamed:@"cornerBg"] stretchableImageWithLeftCapWidth:169.5 topCapHeight:93.5]];
    [self.bannerScroll addSubview:secondBg];
    
    {
        UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(26, 130/2-120/2, 113, 120)];
        [icon setImage:[UIImage imageNamed:@"leading_second"]];
        [secondBg addSubview:icon];
        
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(168, 26, 90, 23)];
        title.font = [UIFont fontWithName:@"PingFang-SC-SemiBold" size:24];
        title.textColor = Color(@"333333");
        title.text = @"第二步";
        [secondBg addSubview:title];
        
        UILabel *subtitle = [[UILabel alloc]initWithFrame:CGRectMake(168, 73, 150, 17)];
        subtitle.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:16];
        subtitle.textColor = Color(@"333333");
        subtitle.text = @"设置时间，开始光疗";
        [secondBg addSubview:subtitle];
    }
    
    self.bannerScroll.contentSize = CGSizeMake((DEVICE_WIDTH-40)*2, 130);
}
#pragma mark - instance
-(UIScrollView*)bannerScroll{
    if (!_bannerScroll) {
        _bannerScroll = [[UIScrollView alloc]init];
        _bannerScroll.backgroundColor = [UIColor clearColor];
        _bannerScroll.pagingEnabled = YES;
    }
    return _bannerScroll;
}
-(IBAction)helpBtnPressed:(id)sender{
    HelpListViewController *help = [[HelpListViewController alloc]init];
    [self.rt_navigationController pushViewController:help animated:YES complete:nil];
}
-(IBAction)kefuBtnPressed:(id)sender{
    GMWKWebViewController *webView = [[GMWKWebViewController alloc]init];
    webView.url = @"https://tb.53kf.com/code/app/A67889FD499F556C15F78A820D19647E9/1?device=ios";
    [self.rt_navigationController pushViewController:webView animated:YES complete:nil];
}
-(IBAction)linkBtnPressed:(id)sender{
    if (self.currentDeviceModel) {
        BeautyInViewController *beautyIn = [[BeautyInViewController alloc]init];
        beautyIn.deviceModel = self.currentDeviceModel;
        [self.rt_navigationController pushViewController:beautyIn animated:YES complete:nil];
    }else{
        MyDeviceListViewController *deviceList = [[MyDeviceListViewController alloc]init];
        [self.rt_navigationController pushViewController:deviceList animated:YES complete:nil];
    }
}
-(IBAction)goCoursePage:(id)sender{
    MyCourseViewController *myCourse = [[MyCourseViewController alloc]init];
    [self.rt_navigationController pushViewController:myCourse animated:YES complete:nil];
}
-(DeviceViewModel*)deviceInfoViewModel{
    if (!_deviceInfoViewModel) {
        _deviceInfoViewModel = [[DeviceViewModel alloc]init];
    }
    return _deviceInfoViewModel;
}
-(SmartLinkExportObject*)linkObj{
    if (!_linkObj) {
        _linkObj = [[SmartLinkExportObject alloc]init];
        _linkObj.delegate = self;
    }
    return _linkObj;
}
#pragma mark - 扫描设备回调
-(void)onDeviceScaned:(NSDictionary *)deviceDic{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"扫描局域网设备回调" message:[NSString stringWithFormat:@"%@",deviceDic] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:alert animated:YES completion:nil];
    });
}
- (NSString *)internetStatus {
    
    Reachability *reachability   = [Reachability reachabilityWithHostName:@"www.apple.com"];
    
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    
    NSString *net = @"WIFI";
    
    switch (internetStatus) {
            
        case ReachableViaWiFi:
            
            net = @"WIFI";
            
            break;
            
        case ReachableViaWWAN:
            
            net = @"蜂窝数据";
            
            //            net = [self getNetType ];   //判断具体类型
            
            break;
            
        case NotReachable:
            
            net = @"当前无网路连接";
            
        default:
            net = @"当前无网路连接";
            break;
            
    }
    
    return net;
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
